package com.ke.jdyd.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextClock;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.request.target.PreloadTarget;
import com.ke.jdyd.R;
import com.ke.jdyd.presenter.DetailPresenter;
import com.ke.jdyd.ui.adapter.DetailImageAdapter;
import com.ke.jdyd.ui.adapter.HeaderPagerAdapter;

import java.util.List;

public class DetailActivity extends AppCompatActivity implements DetailContract.View {

    //Presenter
    private DetailContract.Presenter mPresenter;

    //Views
    private View backView;
    private View loadingView;
    private TextView errMessageView;
    private TextView reloadView;
    private View progressView;

    private ViewPager goodCoverVp;
    private TextView goodPriceTv;
    private TextView goodNameTv;
    private TextView goodPostageTv;
    private TextView goodDeliveryPlaceTv;
    private RecyclerView goodPicsRv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();

        mPresenter = new DetailPresenter(getIntent().getExtras(), this);

        setContentView(R.layout.acitivty_detail);
        backView = findViewById(R.id.back_iv);
        loadingView = findViewById(R.id.loading_view);
        errMessageView = findViewById(R.id.error_message_tv);
        reloadView = findViewById(R.id.reload_tv);
        progressView = findViewById(R.id.progress);

        goodCoverVp = findViewById(R.id.cover_vp);
        goodPriceTv = findViewById(R.id.good_price_tv);
        goodNameTv = findViewById(R.id.good_name_tv);
        goodPostageTv = findViewById(R.id.postage_tv);
        goodDeliveryPlaceTv = findViewById(R.id.place_tv);
        goodPicsRv = findViewById(R.id.pics_rv);

        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mPresenter.onViewCreated();
    }

    @Override
    public void showLoading() {
        loadingView.setVisibility(View.VISIBLE);
        progressView.setVisibility(View.VISIBLE);

        errMessageView.setVisibility(View.GONE);
        reloadView.setVisibility(View.GONE);
    }

    @Override
    public void hideLoading() {
        loadingView.setVisibility(View.GONE);
    }

    @Override
    public void showError(String message, boolean reload) {
        loadingView.setVisibility(View.VISIBLE);
        progressView.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(message)) {
            errMessageView.setText(message);
            errMessageView.setVisibility(View.VISIBLE);
        }
        if (reload) {
            reloadView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.reload();
                }
            });
        }
        reloadView.setVisibility(reload ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showGoodCover(List<String> urls) {
        if (urls != null && urls.size() > 0) {
            goodCoverVp.setAdapter(new HeaderPagerAdapter(urls, null));
        }
    }

    @Override
    public void showGoodInfo(String goodName, float goodPrice, String postage, String delivery_place) {
        goodNameTv.setText(goodName);
        goodPriceTv.setText(String.format("￥%s", goodPrice));
        goodPostageTv.setText(String.format("快递:%s", postage));
        goodDeliveryPlaceTv.setText(delivery_place);
    }

    @Override
    public void showGoodImages(List<String> urls) {
        if (urls != null && urls.size() > 0) {
            goodPicsRv.setLayoutManager(new LinearLayoutManager(this));
            goodPicsRv.setAdapter(new DetailImageAdapter(this, urls));
        }
    }
}
